package com.jonwa.dansRPG;

import com.jonwa.dansRPG.skills.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.*;

public class DansRPG extends JavaPlugin implements Listener {

    private final Map<String, Skill> registeredSkills = new HashMap<>();
    private final Map<UUID, Long> cooldowns = new HashMap<>();

    @Override
    public void onEnable() {
        getLogger().info("技能插件已启动!");
        getServer().getPluginManager().registerEvents(this, this);

        // 注册技能
        registerSkill(new FireballSkill());
        registerSkill(new HealSkill());
    }

    // 注册技能
    private void registerSkill(Skill skill) {
        registeredSkills.put(skill.getName().toLowerCase(), skill);
        getLogger().info("已注册技能: " + skill.getName());
    }

    // 玩家右键触发技能
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();

        // 检查是否拿着技能物品（这里以木棍为例）
        if (event.getItem() != null && event.getItem().getType().name().equals("STICK")) {
            // 获取绑定的技能（默认火球术）
            Skill skill = registeredSkills.get("fireball");
            if (skill == null) return;

            // 检查冷却
            if (!isOnCooldown(player, skill)) {
                skill.execute(player);
                applyCooldown(player, skill);
            } else {
                player.sendMessage("§c技能冷却中！剩余: " +
                        getRemainingCooldown(player, skill) + "秒");
            }
        }
    }

    // 应用冷却时间
    private void applyCooldown(Player player, Skill skill) {
        cooldowns.put(player.getUniqueId(),
                System.currentTimeMillis() + skill.getCooldown() * 1000L);
    }

    // 检查是否冷却
    private boolean isOnCooldown(Player player, Skill skill) {
        return cooldowns.containsKey(player.getUniqueId()) &&
                cooldowns.get(player.getUniqueId()) > System.currentTimeMillis();
    }

    // 获取剩余冷却时间
    private long getRemainingCooldown(Player player, Skill skill) {
        long remaining = (cooldowns.get(player.getUniqueId()) - System.currentTimeMillis()) / 1000;
        return Math.max(0, remaining);
    }

    // 指令切换技能示例: /skill set fireball
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) return false;

        if (cmd.getName().equalsIgnoreCase("skill")) {
            if (args.length > 0 && args[0].equalsIgnoreCase("set")) {
                Player player = (Player) sender;
                String skillName = args[1].toLowerCase();

                if (registeredSkills.containsKey(skillName)) {
                    // 这里可以存到玩家数据中
                    player.sendMessage("§a已切换技能: " + skillName);
                    return true;
                }
            }
        }
        return false;
    }
}